<?php
/**
 * @version $Id: view.html.php 24 2009-03-29 13:19:58Z der.el.kuku $
 * @package    WikiInclude
 * @subpackage
 * @author     EasyJoomla {@link http://www.easy-joomla.org Easy-Joomla.org}
 * @author     Nikolai Plath {@link http://www.easy-joomla.org}
 * @author     Created on 26-Mar-2009
 */

//--No direct access
defined( '_JEXEC' ) or die( '=;)' );

jimport( 'joomla.application.component.view');

/**
 * HTML View class for the WikiInclude Component
 *
 * @package    WikiInclude
 * @subpackage Views
 */

class WikiIncludesViewWikiInclude extends JView
{
	/**
	 * WikiInclude view display method
	 *
	 * @return void
	 **/
	function display($tpl = null)
	{
		$task = JRequest::getCmd('task');

		switch( $task )
		{
			case 'edit':
				//get the WikiInclude
				$WikiInclude =& $this->get('Data');
				$isNew = ($WikiInclude->id < 1);

				$text = $isNew ? JText::_( 'New' ) : JText::_( 'Edit' );
				JToolBarHelper::title(  'WikiInclude: <small><small>[ ' . $text.' ]</small></small>' );
				JToolBarHelper::save();
				if ($isNew)  {
					JToolBarHelper::cancel();
				} else {
					// for existing items the button is renamed `close`
					JToolBarHelper::cancel( 'cancel', JText::_('Close') );
				}

				$this->assignRef('WikiInclude', $WikiInclude);
				break;
					
			case 'preview':
				$dbVersion =& $this->get('Data');
				$id = JRequest::getInt('cid');
				if( ! $id )
				{
					JError::raiseWarning(100, 'no id');
					return;
				}
				require_once JPATH_COMPONENT.DS.'helpers'.DS.'includer.php';
				$wikiVersion = WikiIncluder::getPage('', $id);

				if( ! $wikiVersion->text )
				{
					echo $wikiVersion->error;
				}
				else
				{
					$dbVer = explode("\n", htmlentities($dbVersion->text));
					$wikiVer = explode("\n", htmlentities($wikiVersion->text));
					require_once JPATH_COMPONENT.DS.'helpers'.DS.'DifferenceEngine.php';
					//--we are adding a blank line to the end.. this is somewhat 'required' by PHPdiff
					if( $dbVer[count($dbVer)-1] != '')
					{
						$dbVer[] = '';
					}
					if( $wikiVer[count($wikiVer)-1] != '')
					{
						$wikiVer[] = '';
					}

					$dwDiff = new Diff( $dbVer, $wikiVer);
					$dwFormatter = new TableDiffFormatter();
					$diff =  $dwFormatter->format($dwDiff);




					$this->assignRef('diff', $diff);
					$this->assignRef('wikiVersion', $wikiVersion);
				}
				break;
		}//switch
		parent::display($tpl);
	}// function
}// class
